using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

public class CRTRenderPassFeature : ScriptableRendererFeature
{
    [System.Serializable]
    public class Settings { // 配置项
        public RenderPassEvent renderPassEvent = RenderPassEvent.AfterRenderingOpaques;
        public Material blitMaterial = null;
    }
    
    class CRTRenderPass : ScriptableRenderPass
    {
        private string profilerTag; // 分析器标签, 在Frame Debugger中可以看到该标签
        private RenderTargetIdentifier source; // 源缓存标识
        private RenderTargetIdentifier destination; // 目标缓存标识
        private int destinationId; // 目标缓存id
        private FilterMode filterMode; // 纹理采样滤波模式, 取值有: Point、Bilinear、Trilinear
        
        public CRTRenderPass(string tag) {
            profilerTag = tag;
            destinationId = Shader.PropertyToID("_TempRT");
        }
        
        public Settings settings = new Settings(); // 设置
        // This method is called before executing the render pass.
        // It can be used to configure render targets and their clear state. Also to create temporary render target textures.
        // When empty this render pass will render to the active camera render target.
        // You should never call CommandBuffer.SetRenderTarget. Instead call <c>ConfigureTarget</c> and <c>ConfigureClear</c>.
        // The render pipeline will ensure target setup and clearing happens in a performant manner.
        public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
        {
            RenderTextureDescriptor blitTargetDescriptor = renderingData.cameraData.cameraTargetDescriptor;
            blitTargetDescriptor.depthBufferBits = 0;
            ScriptableRenderer renderer = renderingData.cameraData.renderer;
            source = renderer.cameraColorTarget;
            cmd.GetTemporaryRT(destinationId, blitTargetDescriptor, filterMode);
            destination = new RenderTargetIdentifier(destinationId);
        }

        // Here you can implement the rendering logic.
        // Use <c>ScriptableRenderContext</c> to issue drawing commands or execute command buffers
        // https://docs.unity3d.com/ScriptReference/Rendering.ScriptableRenderContext.html
        // You don't have to call ScriptableRenderContext.submit, the render pipeline will call it at specific points in the pipeline.
        public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
        {
            CommandBuffer cmd = CommandBufferPool.Get(profilerTag);
            Blit(cmd, source, destination, settings.blitMaterial);
            Blit(cmd, destination, source);
            context.ExecuteCommandBuffer(cmd);
            CommandBufferPool.Release(cmd);
        }

        // Cleanup any allocated resources that were created during the execution of this render pass.
        public override void OnCameraCleanup(CommandBuffer cmd)
        {
            
        }

        public override void FrameCleanup(CommandBuffer cmd)
        {
            if (destinationId != -1) {
                cmd.ReleaseTemporaryRT(destinationId);
            }
        }
    }
    
    public Settings settings = new Settings(); // 设置
    CRTRenderPass CRTPass;

    /// <inheritdoc/>
    public override void Create()
    {
        CRTPass = new CRTRenderPass("CRT Pass");

        // Configures where the render pass should be injected.
        CRTPass.renderPassEvent = RenderPassEvent.AfterRenderingOpaques;
    }

    // Here you can inject one or multiple render passes in the renderer.
    // This method is called when setting up the renderer once per-camera.
    public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    {
        if (settings.blitMaterial == null) {
            return;
        }
        CRTPass.renderPassEvent = settings.renderPassEvent;
        CRTPass.settings = settings;
        renderer.EnqueuePass(CRTPass);
    }
}


